package com.google.code.mircle.core.transfers;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.io.FileUtils;

import com.google.code.mircle.core.Worker.DownloadState;

public class DirectDownloadPanel extends JPanel {

    Transfer t;
    JLabel lblNewLabel_1 = new JLabel("false");
    JLabel lblNewLabel_3 = new JLabel("New label");
    JButton btnRedownload = new JButton("Download Again");
    JButton btnDeleteFile = new JButton("Delete File");
    private final JLabel lblWeb = new JLabel("Web");
    private final LinkLabel lblWeb_1 = new LinkLabel("Web URL");

    public DirectDownloadPanel() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0 };
        setLayout(gridBagLayout);

        JLabel lblDirectDownload = new JLabel();
        lblDirectDownload.setText("Direct Download");
        GridBagConstraints gbc_lblDirectDownload = new GridBagConstraints();
        gbc_lblDirectDownload.insets = new Insets(0, 0, 5, 5);
        gbc_lblDirectDownload.anchor = GridBagConstraints.NORTHWEST;
        gbc_lblDirectDownload.gridx = 0;
        gbc_lblDirectDownload.gridy = 0;
        add(lblDirectDownload, gbc_lblDirectDownload);

        btnRedownload.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                t.stop();
                FileUtils.deleteQuietly(t.getWorker().getTarget());
                t.start();
            }
        });
        GridBagConstraints gbc_btnRedownload = new GridBagConstraints();
        gbc_btnRedownload.insets = new Insets(0, 0, 5, 5);
        gbc_btnRedownload.gridx = 1;
        gbc_btnRedownload.gridy = 0;
        add(btnRedownload, gbc_btnRedownload);

        btnDeleteFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                FileUtils.deleteQuietly(t.getWorker().getTarget());
            }
        });
        GridBagConstraints gbc_btnDeleteFile = new GridBagConstraints();
        gbc_btnDeleteFile.insets = new Insets(0, 0, 5, 0);
        gbc_btnDeleteFile.gridx = 2;
        gbc_btnDeleteFile.gridy = 0;
        add(btnDeleteFile, gbc_btnDeleteFile);

        JLabel lblNewLabel = new JLabel("Done?:");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.anchor = GridBagConstraints.NORTHWEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 1;
        add(lblNewLabel, gbc_lblNewLabel);

        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1.gridx = 1;
        gbc_lblNewLabel_1.gridy = 1;
        add(lblNewLabel_1, gbc_lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("targetFile");
        GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
        gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_2.gridx = 0;
        gbc_lblNewLabel_2.gridy = 2;
        add(lblNewLabel_2, gbc_lblNewLabel_2);

        GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
        gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_3.gridx = 1;
        gbc_lblNewLabel_3.gridy = 2;
        add(lblNewLabel_3, gbc_lblNewLabel_3);

        GridBagConstraints gbc_lblWeb = new GridBagConstraints();
        gbc_lblWeb.insets = new Insets(0, 0, 0, 5);
        gbc_lblWeb.gridx = 0;
        gbc_lblWeb.gridy = 3;
        add(lblWeb, gbc_lblWeb);

        GridBagConstraints gbc_lblWeb_1 = new GridBagConstraints();
        gbc_lblWeb_1.insets = new Insets(0, 0, 0, 5);
        gbc_lblWeb_1.gridx = 1;
        gbc_lblWeb_1.gridy = 3;
        add(lblWeb_1, gbc_lblWeb_1);

    }

    public void setTransfer(Transfer t) {
        this.t = t;

        DirectDownload dd = (DirectDownload) t.getWorker();

        if (dd.state.ee != null)
            lblNewLabel_1.setText(dd.state.ee.getMessage());
        else
            lblNewLabel_1.setText("" + (dd.state.downloadState == DownloadState.DOWNLOAD_COMPLETE));

        if (dd.getTarget() != null)
            lblNewLabel_3.setText(dd.getTarget().toString());
        else
            lblNewLabel_3.setText("");

        btnRedownload.setEnabled(!t.isAlive());
        File f = dd.getTarget();
        if (f != null)
            btnDeleteFile.setEnabled(f.exists());
        else
            btnDeleteFile.setEnabled(false);

        if (dd.state.web != null)
            lblWeb_1.setText(dd.state.web);
        else
            lblWeb_1.setText("");
    }
}
